package com.yunxi.service.dubboService.impl.sto;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.util.StringUtil;
import com.yunxi.core.util.BeanTools;
import com.yunxi.model.sto.VehicleOffline;
import com.yunxi.service.localService.sto.VehicleOfflineService;
import com.yunxi.service.localService.sto.VehicleStockMService;
import com.yunxi.sto.pojo.*;
import com.yunxi.sto.service.VehicleOfflineDubboService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 车辆下线数据 服务实现类
 * </p>
 *
 * @author caoYong
 * @since 2019-05-21
 */
public class VehicleOfflineDubboServiceImpl implements VehicleOfflineDubboService {

    @Autowired
    @Qualifier(value = "vehicleOfflineServiceImpl")
    VehicleOfflineService vehicleOfflineService;

    @Autowired
    @Qualifier(value = "vehicleStockMServiceImpl")
    VehicleStockMService vehicleStockMService;


    @Override
    public VehicleOfflineVO getById(String id)  {
        VehicleOffline vehicleOffline = vehicleOfflineService.getById(id);
        VehicleOfflineVO vehicleOfflineVO = new VehicleOfflineVO();
        BeanTools.copyProperties(vehicleOfflineVO,vehicleOffline);
        return vehicleOfflineVO;
    }

    /**
     * 通过主键id获取详情
     * @param id
     * @return
     */
    @Override
    public VehicleOfflineVO getDetailById(String id) {
        return vehicleOfflineService.getDetailById(id);
    }

    @Override
    public void saveOrUpdate(VehicleOfflineVO vehicleOfflineVO)  {
        VehicleOffline vehicleOffline = new VehicleOffline();
        BeanUtils.copyProperties(vehicleOfflineVO,vehicleOffline);
        vehicleOfflineService.saveOrUpdate(vehicleOffline);
    }

    @Override
    public void removeById(String id) {
        vehicleOfflineService.removeById(id);
    }

    @Override
    public PageInfo<VehicleOfflineQueryVO> findPageByVO(VehicleOfflineQueryVO vehicleOfflineQueryVO, Integer pageNum, Integer pageSize) {
        return vehicleOfflineService.pageByVo(vehicleOfflineQueryVO, pageNum, pageSize);
    }

    @Override
    public List listByVo(VehicleOfflineQueryVO v) {
        return vehicleOfflineService.listByVo(v);
    }

    @Override
    public VehicleOfflineVO getOne(Object wrapper) {
        VehicleOffline vehicleOffline = vehicleOfflineService.getOne((QueryWrapper<VehicleOffline>) wrapper);
        VehicleOfflineVO vehicleOfflineVO = new VehicleOfflineVO();
        BeanUtils.copyProperties(vehicleOffline,vehicleOfflineVO);
        return vehicleOfflineVO;
    }

    @Override
    public void updateByVin(VehicleOfflineVO voo) {
        vehicleOfflineService.updateByVin(voo);
    }

    /**
     * VIN码模糊查询
     * @param vin
     * @return
     */
    @Override
    public List<String> getVinList(String vin) {
        VehicleOfflineVO vehicleOfflineVO = new VehicleOfflineVO();
        vehicleOfflineVO.setVin(vin);
        List<String> vinList = vehicleOfflineService.getVinList(vehicleOfflineVO);
        if(StringUtils.isNotEmpty(vin)){
            vinList.add(0,vin);
        }
        return vinList;
    }

    @Override
    public boolean setVehicleShareByVin(VehicleOfflineSetShareReqVO vo) {
        QueryWrapper qw = new QueryWrapper();
        qw.eq("VIN",vo.getVin());
        VehicleOffline vehicleOffline = new VehicleOffline();
        vehicleOffline.setIsShare(vo.getIsShare());
        return vehicleOfflineService.update(vehicleOffline,qw);
    }

    /**
     * 共享车查询
     * @param vo
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public PageInfo<VehicleOfflineShareRespVO> queryShareVehicle(VehicleOfflineShareReqVO vo,int pageNum,int pageSize){
        return vehicleOfflineService.queryShareVehicle(vo,pageNum,pageSize);
    }

    /**
     * 根据计划号设置车辆用途
     * @param vehicleOfflinePurposeVO
     * @throws Exception
     */
    @Override
    public void vehiclePurpose(VehicleOfflinePurposeVO vehicleOfflinePurposeVO) throws Exception {
        String zordNo = vehicleOfflinePurposeVO.getZordNo();
        Integer purpose = vehicleOfflinePurposeVO.getPurpose();
        if(StringUtils.isNotEmpty(zordNo) && purpose != null){
            QueryWrapper<VehicleOffline> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("ZORD_NO",zordNo);
            VehicleOffline vehicleOffline = vehicleOfflineService.getOne(queryWrapper);
            if(vehicleOffline != null){
                vehicleOffline.setVehiclePurpose(purpose);
                vehicleOfflineService.saveOrUpdate(vehicleOffline);
            }
            else{
                throw new Exception("操作失败,无法根据计划号:"+zordNo+"找到对应车辆!");
            }
        }
        else{
            throw new Exception("操作失败,计划单号或车辆用途不能为空!");
        }
    }

    @Override
    public VehicleOfflineVO getVoByVin(String vin) {
        return vehicleOfflineService.getVoByVin(vin);
    }

}
